﻿// Implemented by 'Imran Gani' on 11-Mar-2013.

$(document).ready(function () {
    if (checkExistenceCookies())
        handleCheckBoxList();
    doSearch();
});

function checkExistenceCookies() {
    var ret = false;
    var curProp = getMB_property_check("ctl00_ContentPlaceHolder3_chkSelectDeselectAll", "checked");
    if (curProp != null)
        ret = true;
    return ret;
}

function handleCheckBoxList() {
    var curProp = getMB_property_check("ctl00_ContentPlaceHolder3_chkSelectDeselectAll", "checked");
    if (curProp != null && curProp == "1")
        $("INPUT[type='checkbox']").attr('checked', true);
    else {
        var chkBox = document.getElementById("ctl00_ContentPlaceHolder3_cbl_search_in_obj");
        var options = chkBox.getElementsByTagName("input");
        var listOfSpans = chkBox.getElementsByTagName("span");
        for (var i = 0; i < options.length; i++) {
            curProp = getMB_property_check(listOfSpans[i].attributes["id"].value, "checked");
            if (curProp == null)
                curProp = "0";
            options[i].checked = curProp == "1" ? true : false;
        }
    }
}

function getMB_property_check(elem, propname) {
    var ret = null;
    try {
        if ($.cookie('mb_prop_cache'))//have MaxBlox property cache
        {
            var props = jQuery.parseJSON($.cookie('mb_prop_cache'));
            var propertyElem = null;
            for (var j = 0; j < props.mb_properties.length; j++) {
                if (props.mb_properties[j].Key == elem + "_" + propname) {
                    ret = props.mb_properties[j].value;
                    break;
                }
            }
        }
    }
    catch (Exgp) { }
    return ret;
}

function setMB_property_check(elem, propname, propvalue) {
    try {
        var props = null;
        if ($.cookie('mb_prop_cache'))
            props = jQuery.parseJSON($.cookie('mb_prop_cache'));
        else
            props = jQuery.parseJSON('{"mb_properties":[        {"key": "elem_prop","value" : "123"}    ]}');
        var mb_key = elem + "_" + propname;
        var found = false;
        for (var j = 0; j < props.mb_properties.length; j++) {
            if (props.mb_properties[j].Key == mb_key) {
                if (propvalue == "1")
                    props.mb_properties[j].value = propvalue;
                else if (elem == "ctl00_ContentPlaceHolder3_chkSelectDeselectAll")
                    props.mb_properties[j].value = propvalue;
                else
                    props.mb_properties.splice(j, 1);
                found = true;
                break;
            }
        }
        if (found == false && (propvalue != "0" || elem == "ctl00_ContentPlaceHolder3_chkSelectDeselectAll"))
            props.mb_properties.push({ "Key": mb_key, 'value': propvalue });
        var property_cache_string = JSON.stringify(props);
        //alert(property_cache_string);
        $.cookie('mb_prop_cache', property_cache_string, { expires: 365 });
    }
    catch (Exset) { }
}

function selectallcheckbox() {
    if (document.getElementById("ctl00_ContentPlaceHolder3_chkSelectDeselectAll").checked)
        $("INPUT[type='checkbox']").attr('checked', true);
    else {
        $("INPUT[type='checkbox']").attr('checked', false);
        var chkBox = document.getElementById("ctl00_ContentPlaceHolder3_cbl_search_in_obj");
        var options = chkBox.getElementsByTagName("input");
        var listOfSpans = chkBox.getElementsByTagName("span");
        for (var i = 0; i < options.length; i++) {
            setMB_property_check(listOfSpans[i].attributes["id"].value, "checked", "0");
        }
    }
    setMB_property_check("ctl00_ContentPlaceHolder3_chkSelectDeselectAll", "checked", (document.getElementById("ctl00_ContentPlaceHolder3_chkSelectDeselectAll").checked ? "1" : "0"));
}

function Deselectallcheckbox() {
    document.getElementById("ctl00_ContentPlaceHolder3_chkSelectDeselectAll").checked = false;
    setMB_property_check("ctl00_ContentPlaceHolder3_chkSelectDeselectAll", "checked", "0");
    var chkBox = document.getElementById("ctl00_ContentPlaceHolder3_cbl_search_in_obj");
    var options = chkBox.getElementsByTagName("input");
    var listOfSpans = chkBox.getElementsByTagName("span");
    for (var i = 0; i < options.length; i++) {
        setMB_property_check(listOfSpans[i].attributes["id"].value, "checked", (options[i].checked?"1":"0"));
    }
}
/********************End of cookies******************/

function showObjDiv() {
    $("#div_search_in_obj").slideDown("", function () { $(this).css("display", "block"); });
}
function HideObjDiv() {
    $("#div_search_in_obj").slideUp("", function () { $(this).css("display", "none"); });
}


function mb_processSearchGo() {
    var InputText = document.getElementById("ctl00_ContentPlaceHolder3_txt_search");
    var mb_loca = document.location.href;
    var curVariables = mb_getCurUrlVariables();
    var newVariables = [], hash;
    //add search flag
    if (InputText.value != "") {
        newVariables.push('val');
        newVariables['val'] = /*InputText.value; /*/  escape(InputText.value);
    }
    else {
        $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Enter the value to search.</div>");
        return false;
    }
    //remove old variables when new exists
    var mb_go_url = window.location.href.substr(0, window.location.href.indexOf('?'));
    for (var i = 0; i < newVariables.length; i++) {
        if (curVariables[newVariables[i]] == undefined)//not exists
        {
            curVariables.push(newVariables[i]);
            curVariables[newVariables[i]] = newVariables[newVariables[i]];
        }
        else //exists
            curVariables[newVariables[i]] = newVariables[newVariables[i]]; //fix this later
    }
    //
    for (var i = 0; i < curVariables.length; i++) {
        mb_go_url += (i == 0 ? '?' : '&') + curVariables[i] + "=" + curVariables[curVariables[i]];
    }
    document.location.href = mb_go_url;
    //return false;
}
function mb_getCurUrlVariables() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];// escape(hash[1]);
    }
    return vars;
}

/********************Search process*******************/

function doSearch() {
    try {
        var DynamicDivResults = document.getElementById("ctl00_ContentPlaceHolder3_DynamicDataResults");
        DynamicDivResults.innerHTML = "";
        var curVariables = mb_getCurUrlVariables();
        var InputText = document.getElementById("ctl00_ContentPlaceHolder3_txt_search");
        InputText.value = curVariables["val"] == undefined ? "" : unescape(curVariables["val"]);
        if (InputText != null && InputText.value != "") {
            $("#ctl00_ContentPlaceHolder3_lblMessage").html("");
            $("#ctl00_ContentPlaceHolder3_td_process").html(""); 
            var chkBox = document.getElementById("ctl00_ContentPlaceHolder3_cbl_search_in_obj");
            var options = chkBox.getElementsByTagName("input");
            var TestList = chkBox.getElementsByTagName("label");
            var listOfSpans = chkBox.getElementsByTagName("span");
            var pgDetails = "";
            var ObjCheck = false;
            for (var i = 0; i < options.length; i++) {
                //alert(options[i].id);
                if (options[i].checked) {
                    ObjCheck = true;
                    var iObjectID = listOfSpans[i].attributes["id"].value;
                    var PageIconPath;
                    var ObjectDetails = jQuery.parseJSON($("#ctl00_ContentPlaceHolder3_hfObjectDetails").val());
                    for (var k = 0; k < ObjectDetails.length; k++) {
                        if (ObjectDetails[k]["ObjId"] == iObjectID)
                            PageIconPath = ObjectDetails[k]["IconPath"];
                    }
                    GenerateObjectHeader(TestList[i].innerHTML, iObjectID, DynamicDivResults, PageIconPath);
                    doAjaxProcess(InputText.value, iObjectID, pgDetails);
                }
            }
            if(!ObjCheck)
                $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Select the page(s) to be searched in.</div>");
        }
//        else 
//            $("#ctl00_ContentPlaceHolder3_lblMessage").html("<div class=\"mbMessage\"><img id=\"imgInfo\" src=\"Images/info.gif\" alt=\"Information\" /> &nbsp;Enter the value to search.</div>");
    }
    catch(e)
    {}
}

function doAjaxProcess(srhText, iObjectID, pgDetails) {
    $.ajax({
        url: "mb_ajaxhandler.aspx?action=data_search&data=" + escape(srhText) + "&val=" + iObjectID + "&pgInfo=" + pgDetails,
        dataType: "json",
        cache: false,
        beforeSend: function () { $('#bt_GoSearch').attr('disabled', 'disabled'); LoadingImage(iObjectID, "REQUEST", ""); },
        success: function (respObj) {
            if (respObj.NORECORD) {
                var msg = respObj.NORECORD;
                LoadingImage(iObjectID, "RESPONSE", msg);
            }
            else
                BuildObjectItems(srhText, respObj, iObjectID);
        },
        complete: function () { $("#ctl00_ContentPlaceHolder3_td_process").html("Results for '" + srhText + "'"); },
        error: function (msg) { $("#ctl00_ContentPlaceHolder3_Object" + iObjectID + "_CAPTION").html(""); }
    });
    $('#bt_GoSearch').removeAttr('disabled');
}

/***********Code For Generating the Object Div**********************/

function GenerateObjectHeader(srhObjectName, srhStorageObjectId, dynamicObjectDiv, PageIconPath) {
    if (dynamicObjectDiv) {
        var catHolderDiv = new createObjectDiv(srhObjectName, srhStorageObjectId, PageIconPath);
        catHolderDiv.addObjectCaption();
        catHolderDiv.addObjectTable();
        catHolderDiv.addObjectMore();
        dynamicObjectDiv.appendChild(catHolderDiv.getCreatedObjectDiv());
    }
}

function createObjectDiv(ObjectName, srhStorageObjectId, PageIconPath) {
    this.holderdiv = document.createElement("div");
    this.holderdiv.style.margin = "0px 0px 10px 0px";
    this.sOjectName = ObjectName;
    this.ObjectStorageValue = srhStorageObjectId;
    this.SrcPageIcon = PageIconPath;
}
createObjectDiv.prototype.addObjectCaption = function () {
    var CatCaptionDiv = document.createElement("div");
    CatCaptionDiv.className = "grid_header_light";
    CatCaptionDiv.style.align = "midle"
    //CatCaptionDiv.className = "Dynamic_PageHeaderRow";
    CatCaptionDiv.id = "ctl00_ContentPlaceHolder3_Object" + this.ObjectStorageValue + "_CAPTION";

    if (this.sOjectName.trim() == "")
        CatCaptionDiv.innerHTML = "&nbsp;<img src=\"" + this.SrcPageIcon + "\" style=\"vertical-align:middle;\" \>&nbsp;[--No Object--]";
    else
        CatCaptionDiv.innerHTML = "&nbsp;<img src=\"" + this.SrcPageIcon + "\" style=\"vertical-align:middle;\" \>&nbsp;&nbsp;" + this.sOjectName;

    this.holderdiv.appendChild(CatCaptionDiv);
}
createObjectDiv.prototype.addObjectTable = function () {
    var CatTable = document.createElement("table");
    CatTable.id = "ctl00_ContentPlaceHolder3_Object" + this.ObjectStorageValue + "_TABLE";

    CatTable.width = "100%";
    //CatTable.height = "1%";
    CatTable.cellPadding = 0;
    CatTable.cellSpacing = 0;
    CatTable.border = 0;
    var CatTableBody = document.createElement('tbody');
    CatTable.appendChild(CatTableBody);
    this.holderdiv.appendChild(CatTable);
}
createObjectDiv.prototype.addObjectMore = function () {
    var CatMoreDiv = document.createElement("div");
    CatMoreDiv.innerHTML = "&nbsp;";
    CatMoreDiv.className = "CategoryMoreDiv";
    CatMoreDiv.id = "ctl00_ContentPlaceHolder3_Object" + this.ObjectStorageValue + "_MORE";
    this.holderdiv.appendChild(CatMoreDiv);
}
createObjectDiv.prototype.getCreatedObjectDiv = function () {
    return this.holderdiv;
}
/******************End of Object Header Function****************/

/********************Common Methods************************/

function LoadingImage(cid, key, msg) {
    var moredivobj = get_object("ctl00_ContentPlaceHolder3_Object" + cid + "_MORE");
    if (key == "REQUEST") {
        if (moredivobj)
            moredivobj.innerHTML = "&nbsp;<img src=\"Images/load.gif\" style=\"width:15px;Padding-top:3;\">&nbsp;&nbsp;Loading...";
    } else if (key == "RESPONSE") {
        if (moredivobj)
            moredivobj.innerHTML = "";
        if (msg != "")
            moredivobj.innerHTML = msg;
    }
}

function get_object(id) {
    var object = null;
    if (document.layers) 
        object = document.layers[id];
    else if (document.all) 
        object = document.all[id];
    else if (document.getElementById) 
        object = document.getElementById(id);
    return object;
}
/********************End Of Common Methods************************/

/********************Code the generate grid list************************/

function BuildObjectItems(srhText, respObj, ObjectId) {
    var pageDetails = "";
    if (respObj.PAGEINFO)
        pageDetails = respObj.PAGEINFO[0].PAGEDETAILS;

    if (respObj.SCHEMA) {
        var colArray = new Array();
        var colDataType = new Array();
        for (var tindex = 0; tindex < respObj.SCHEMA.length; tindex++) {
            colArray[tindex] = respObj.SCHEMA[tindex].CNAME;
            colDataType[tindex] = respObj.SCHEMA[tindex].DTYPE;
        }
    }

    if (colArray.length > 0) {
        var ObjectTableId = "ctl00_ContentPlaceHolder3_Object" + ObjectId + "_TABLE";
        var ObjectMoreId = "ctl00_ContentPlaceHolder3_Object" + ObjectId + "_MORE";
        var ObjectItemTable = get_object(ObjectTableId);

        var rowObj = new generateObjectRow(colArray, colDataType, respObj);
        var trRowArray = rowObj.getObjectRowArray();

        var tbodyObj = ObjectItemTable.getElementsByTagName("tbody")[0];

        /*************** To Find The Child Nodes css Start Index *****************/
        var tBodyChildCount = getTrCountValue(tbodyObj.childNodes);
        var cssGridStartIndex = "0";
        if (parseInt(tBodyChildCount) > 0) {
            if (parseInt(tBodyChildCount) % 2 == 0)
                cssGridStartIndex = "1";
        }
        else {
            var trRowHeader = rowObj.getObjectRowHeader();
            tbodyObj.appendChild(trRowHeader);
        }
        /**************************************************************************/
        if (trRowArray.length > 0) {
            for (var index = 0; index < trRowArray.length; index++) {
                trRowArray[index].className = "Grid_Row" + cssGridStartIndex;

                trRowArray[index].onmouseover = function () {
                    this.className = 'Grid_Scroll_BG'
                }

                trRowArray[index].val = cssGridStartIndex;

                trRowArray[index].onmouseout = function () {
                    this.className = "Grid_Row" + this.val;
                }

                tbodyObj.appendChild(trRowArray[index]);
                cssGridStartIndex = cssGridStartIndex != "0" ? "0" : "1";
            }
            LoadingImage(ObjectId, "RESPONSE", "");
            var MoreDivId = "ctl00_ContentPlaceHolder3_Object" + ObjectId + "_MORE";
            var moredivObj = get_object(MoreDivId);
            if (moredivObj)
                moredivObj.innerHTML = "<a href=\"javascript:;\" onclick=\"doAjaxProcess('" + srhText + "','" + ObjectId + "','" + pageDetails + "')\" ><font color='blue'>More...</font></a>";
        }
        else {
            var trObj = document.createElement('tr');
            trObj.className = "Grid_Row" + cssGridStartIndex;
            for (var cindex = 0; cindex < colArray.length; cindex++) {
                var tdObj = document.createElement('td');
                if (cindex == 1)
                    tdObj.innerHTML = "No Records Available.";
                trObj.appendChild(tdObj);
            }
            rowObj.setStyle(trObj);
            tbodyObj.appendChild(trObj);
            LoadingImage(ObjectId, "RESPONSE", "");
        }
    }
}

function getTrCountValue(tBodyObj) {
    var intCount = 0;
    if (tBodyObj.length > 0) {
        for (var index = 0; index < tBodyObj.length; index++) {
            if (tBodyObj[index].tagName == 'TR')
                intCount++;
        }
    }
    return intCount;
}

function generateObjectRow(colarray, colDatatype, JsonRespObj) {
    this.ColArray = colarray;
    this.ColDataType = colDatatype;
    this.DataRowObj = JsonRespObj.DATA;
    this.PageAccess = JsonRespObj.PAGEACCESS;
    if (JsonRespObj.hasOwnProperty("ACTION"))
        this.Action = JsonRespObj.ACTION;
    this.PagesetId = JsonRespObj.PAGEINFO[0].PAGESETID;
    this.MenuId = JsonRespObj.PAGEINFO[0].MENUID;
    this.EditPage = JsonRespObj.PAGEINFO[0].EDITPAGE;
    this.trRowArray = new Array();
}

generateObjectRow.prototype.getObjectRowHeader = function () {
    var trObj = document.createElement('tr');
    trObj.className = "Grid_Header";
    for (var cindex = 0; cindex < this.ColArray.length; cindex++) {
        var tdObj = document.createElement('td');
        this.setAlignment(tdObj, this.ColDataType[cindex]);
        tdObj.className = "Grid_Header_Cell";
        tdObj.valign = "middle";
        if (this.ColArray[cindex].trim().toUpperCase() != "KEYS")
            tdObj.innerHTML = this.ColArray[cindex] + "&nbsp";
        trObj.appendChild(tdObj);
    }
    return trObj;
}

generateObjectRow.prototype.getObjectRowArray = function () {
    var EditPage = this.EditPage;
    var strMenuRowId = this.MenuId;
    var pgs_id = this.PagesetId;
    if (this.DataRowObj != null) {
        for (var rindex = 0; rindex < this.DataRowObj.length; rindex++) {
            var trObj = document.createElement('tr');
            for (var cindex = 0; cindex < this.ColArray.length; cindex++) {

                var tdObj = document.createElement('td');
                this.setAlignment(tdObj, this.ColDataType[cindex]);
                trObj.appendChild(tdObj);
                if (this.ColArray[cindex].trim().toUpperCase() == "KEYS") {

                    var cellValue = this.DataRowObj[rindex][this.ColArray[cindex]];
                    var firstcellvalue = this.DataRowObj[rindex][this.ColArray[1]];

                    trObj.id = "ctl00_ContentPlaceHolder3_ObjectAF_ROW_ID _" + cellValue;

                    this.appendAccessPermissionIcons(tdObj, cellValue, EditPage, strMenuRowId, pgs_id, firstcellvalue);

                } else {

                    var displayField = "";
                    var cellValue = this.DataRowObj[rindex][this.ColArray[cindex]];
                    displayField = cellValue;
                    if (cindex == 1) {
                        var strText = "";
                        var keyValue = this.DataRowObj[rindex]['keys'];
                        strText = "" + EditPage + "?pgs_id=" + pgs_id + "&PK=" + keyValue;
                        strText += "&mn=" + strMenuRowId + "&VMODE=1";
                        displayField = "<a href=\"" + strText + "\"><font color='blue'>" + displayField + "</font></a>";
                    }
                    if (cellValue == null)
                        tdObj.innerHTML = "&nbsp";
                    else
                        tdObj.innerHTML = displayField + "&nbsp";
                }
            }
            this.setStyle(trObj);
            this.trRowArray[this.trRowArray.length] = trObj;
        }
    }
    return this.trRowArray;
}


generateObjectRow.prototype.appendAccessPermissionIcons = function (tdObj, cellValues, EditPage, strMenuRowId, pgs_id, firstcellvalue) {
    try {
        var bRead = this.PageAccess[0]["READ"] == "True" ? true : false;
        var bCreate = this.PageAccess[0]["CREATE"] == "True" ? true : false;
        var bEdit = this.PageAccess[0]["EDIT"] == "True" ? true : false;
        var bDelete = this.PageAccess[0]["DELETE"] == "True" ? true : false;
        var bAction = this.PageAccess[0]["ACTION"] == "True" ? true : false;

        var tempTableStr = "";
        var EditStr = "";
        var CopyStr = "";
        var DeleteStr = "";
        var appendEditIcons = false;
        tempTableStr = "<table border='0' cellpadding='0' cellspacing='0'><tr>";
        var strText = "";
        if (bEdit) {
            strText = "" + EditPage + "?pgs_id=" + pgs_id + "&PK=" + cellValues;
            strText += "&mn=" + strMenuRowId + "";
            tempTableStr += "<td width=\"25px\"><a style='text-decoration:none;' href=\"" + strText + "\">" + "<img border='0' style='cursor:pointer' onclick=\"javascript:this.src='images/load.gif'\" title='Edit' src=\"images/edit.gif\"></a></td>";
            appendEditIcons = true;
        }
        if (bCreate) {
            strText = "" + EditPage + "?pgs_id=" + pgs_id + "&PK=" + cellValues;
            strText += "&mn=" + strMenuRowId + "&CPY=0";
            tempTableStr += "<td width=\"25px\"><a style='text-decoration:none;' href=\"" + strText + "\">" + "<img border='0' style='cursor:pointer' onclick=\"javascript:this.src='images/load.gif'\" title='Copy' src=\"images/copy.gif\"></a></td>";
            appendEditIcons = true;
        }
        if (bDelete) {
            strText = "";
            tempTableStr += "<td width=\"25px\"><a style='text-decoration:none;' href=\"javascript:;\" onclick=\"DeleteCategoryRow(this,'" + escape(firstcellvalue) + "','" + cellValues + "','" + pgs_id + "','Img_" + cellValues + "_Id')\">" + "<img border='0' id = 'Img_" + cellValues + "_Id' style='cursor:pointer'  title='Delete' src=\"images/delete.gif\"></a></td>";
            appendEditIcons = true;
        }
        if (bAction && this.Action.length > 0) {
            strText = "";
            tempTableStr += "<td width=\"25px\" onmouseover=\"javascript:ShowDivForActions('div_Ac_" + cellValues + "');\" onmouseout=\"javascript:HideDivForActions('div_Ac_" + cellValues + "');\" ><input id=\"ctl00_ContentPlaceHolder3_Ac_" + cellValues + "\" type=\"image\" name=\"ctl00$ContentPlaceHolder3$Ac_" + cellValues + "\" src=\"images/action_16.gif\" style=\"border-width:0px;\"><br><span id=\"ctl00_ContentPlaceHolder3_div_Ac_" + cellValues + "\" class=\"Grid_Row_actions\" style=\"border-color: graytext; border-width: 1px; border-style: solid; display: none; position: absolute;\" onmouseout=\"javascript:HideDivForActions('div_Ac_" + cellValues + "');\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"GrayText\" border=\"0\" style=\"border-bottom:1px;border-top:1px;border-left:1px;border-right:1px;\"><tbody><tr><td nowrap=\"nowrap\"><span class=\"controlFont pgForm_title\" style=\"display:inline-block;width:100%;\"><img src=\"Images/action_16.gif\" style=\"vertical-align:bottom\"><span style=\"vertical-align:bottom\">&nbsp;Actions</span></span></td></tr><tr><td nowrap=\"nowrap\"><span class=\"controlFont Grid_Row_actions\" style=\"display:inline-block;width:100%;\" title=\"" + this.Action[0]["dsc"] + "\">&nbsp; <a href=\"" + this.Action[0]["url"] + "&ID=" + cellValues + "&action=1\">" + this.Action[0]["name"] + "</a> &nbsp;</span><span class=\"Grid_Row_actions\"></span></td></tr></tbody></table></span></td>"
            appendEditIcons = true;
        }
        tempTableStr += "</tr></table>";
        if (appendEditIcons) {
            if (bAction && this.Action.length > 0)
                tdObj.width = '100px';
            else
                tdObj.width = '80px';
            tdObj.innerHTML = tempTableStr;
            //        } else {
            //            tdObj.parentNode.removeChild(tdObj);
        }
    }
    catch (ee) { }
}

generateObjectRow.prototype.setStyle = function (trRowArray) {
    if (trRowArray.childNodes.length > 0) {
        trRowArray.childNodes[0].className = 'leftGrid';
        if (trRowArray.childNodes[0].innerHTML == "")
            trRowArray.childNodes[0].innerHTML = "&nbsp;"
    }

    if (trRowArray.childNodes.length > 1) {
        trRowArray.childNodes[parseInt(trRowArray.childNodes.length) - 1].className = 'rightGrid';
        if (trRowArray.childNodes[parseInt(trRowArray.childNodes.length) - 1].innerHTML == "")
            trRowArray.childNodes[parseInt(trRowArray.childNodes.length) - 1].innerHTML = "&nbsp;"
    }
}

generateObjectRow.prototype.setAlignment = function (tdObj, DataType) {
    switch (DataType) {
        case "BO":
        case "DA":
        case "DT":
        case "TX":
            tdObj.align = "Left";
            break;
        case "CU":
        case "DC":
        case "NU":
        case "AN":
            tdObj.align = "Right";
            break;
        default:
            tdObj.align = "center";
            break;
     }
}

function DeleteCategoryRow(obj, nodeText, key, strPageSetId, imageId) {
    var cmsg = confirm("Are you sure you want to delete " + unescape(nodeText) + " ?\r Note: Child records will also be deleted.");
    if (cmsg) {
        var url = "DisplayGrid.aspx?pgs_id=" + strPageSetId + "&DPK=" + key;
        var tbodyObject = obj.parentNode.parentNode.parentNode;
        var tCurrRowObject = obj.parentNode.parentNode;
        if (imageId != "") {
            var ImageID = get_object(imageId);
            ImageID.src = "images/Load.gif";
        }
        AJAX_call(obj, url, nodeText);
    }
}

function AJAX_call(obj, url, node) {
    if (url == "")
        return;
    else
        url += "&DeleteAjaxTreeCall= true";
    try {
        var xmlHttp_fb;
        var node = node;
        var tree = tree;
        try {
            // Firefox, Opera 8.0+, Safari
            xmlHttp_fb = new XMLHttpRequest();
        } catch (e) {
            // Internet Explorer
            try {
                xmlHttp_fb = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp_fb = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    //alert("Your browser does not support AJAX!");
                    return false;
                }
            }
        }
        xmlHttp_fb.onreadystatechange = function () {
            if (xmlHttp_fb.readyState == 1) {
                //document.getElementById("ctl00_fb_status").innerHTML = "<img src='Images/load.gif' border='0'></img>&nbsp;Submitting...";
            }
            if (xmlHttp_fb.readyState == 4) {
                var responseText = xmlHttp_fb.responseText;
                if (responseText.toUpperCase() == "TRUE") 
                    removeCategoryRowObject(obj);
            }
        }
        var http_date = new Date()
        xmlHttp_fb.open("GET", url, true);
        xmlHttp_fb.send(null);
    } catch (Exxx) {}
}


function removeCategoryRowObject(obj) {
    var tCurrRowObject = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
    //alert ("The Element Id is " + tCurrRowObject.id );
    var tbodyObject = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
    var temptBodyObject = tbodyObject;
    if (tbodyObject) {
        var tCurrRowObject = obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        disableDeletedRow(tbodyObject, tCurrRowObject);
    }
}

function disableDeletedRow(tBodyObject, tDeletedRowObject) {

    var childNodelen = tBodyObject.childNodes.length;
    if (tBodyObject) {
        for (var index = 0; index < tBodyObject.childNodes.length; index++) {
            if (tBodyObject.childNodes[index] === tDeletedRowObject) {
                tBodyObject.childNodes[index].parentNode.removeChild(tBodyObject.childNodes[index]);

                if (getTrPresentStatus(tBodyObject)) {
                    reAssignCss(tBodyObject, index);
                    return;
                } else {
                    removeCategoryHeader(tBodyObject);
                    return;
                }
            }
        }
    }
}

function removeCategoryHeader(tBodyObject) {
    var parentHolderDiv = tBodyObject.parentNode.parentNode;
    parentHolderDiv.parentNode.removeChild(parentHolderDiv);
}

function getTrPresentStatus(tBodyObject) {
    var TrStatus = false;
    for (var index = 0; index < tBodyObject.childNodes.length; index++) {
        if (tBodyObject.childNodes[index].tagName == 'TR') 
            TrStatus = true;
    }
    return TrStatus;
}

function reAssignCss(tBodyObject, rindex) {
    var itemindex = rindex;
    for (index = rindex; index < tBodyObject.childNodes.length; index++) {
        if (tBodyObject.childNodes[index].tagName == 'TR') {
        
            var className = tBodyObject.childNodes[index].className.trim();
            var previouscssIndex = className.substr(8, 1);
            var cssIndex = previouscssIndex == "1" ? "0" : "1";

            tBodyObject.childNodes[index].className = 'Grid_Row' + cssIndex;
            tBodyObject.childNodes[index].val = cssIndex;
            
            tBodyObject.childNodes[index].onmouseout = function () {
                this.className = "Grid_Row" + this.val;
            }
            itemindex++;
        }
    }
}

/********************End of grid list************************/